package com.example.yls.demoa;

import android.app.DownloadManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yls on 2018/12/5.
 */

public class FriendFragment extends Fragment implements AdapterView.OnItemClickListener {
    private  ListView listView;
    private  ArrayAdapter<String> adapter;
    private static MyDatabaseHelper databaseHelper;
    private static SQLiteDatabase database;
    private static    List<String> list;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.read_fragment,container,false);
        list=new ArrayList<>();
      initView(view);
        initAdapter();
        initEvent();
        return  view;
    }

    private void initEvent() {
        listView.setOnItemClickListener(this);
    }

    private void initAdapter() {
      initData();


       adapter=new ArrayAdapter<String>(MainActivity.getActivity(),android.R.layout.simple_list_item_1,list);
        listView.setAdapter(adapter);
    }

    private void initData() {
        databaseHelper=new MyDatabaseHelper(MyApplication.getContext(),"DiaryDatabase",null,2);
        if(database==null){
            createOrOpenDatabase();
        }
        Cursor cursor=database.query("Diary",null,null,null,null,null,null);
        for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
            list.add(cursor.getString(0)+"       "+cursor.getString(2));
        }




    }

    private static void createOrOpenDatabase() {
        database=databaseHelper.getWritableDatabase();
    }

    private void initView(View view) {
           listView=view.findViewById(R.id.listview);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Log.e("e",String.valueOf(i));
    }
}
